package org.csu.group4.jpetstore.service;

import org.csu.group4.jpetstore.mybatis.entity.Account;
import org.csu.group4.jpetstore.mybatis.entity.Orders;
import org.csu.group4.jpetstore.mybatis.entity.ShopCart;
import org.csu.group4.jpetstore.persistence.OrderStatusDAO;
import org.csu.group4.jpetstore.persistence.OrdersDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class OrderService {
    @Autowired
    OrdersDAO ordersDAO;
    @Autowired
    OrderStatusDAO orderStatusDAO;
    @Autowired
    ItemService itemService;

    public void updateOrder(Account account, List<ShopCart> shopCarts){
        Orders orders = new Orders();
        orders.setUserId(account.getUserid());
        orders.setOrderDate(new Date());
        orders.setShipAddr1(account.getAddr1());
        orders.setShipCity(account.getCity());
        orders.setShipState(account.getState());
        orders.setShipZip(account.getZip());
        orders.setShipCountry(account.getCountry());
        orders.setBillAddr1("a");
        orders.setBillCountry("a");
        orders.setBillZip("a");
        orders.setBillState("a");
        orders.setBillCity("a");
        orders.setCourier("sf");
        BigDecimal totalPrice = new BigDecimal(0);
        for (ShopCart shopCart : shopCarts) {
           String itemId = shopCart.getItemId();
           totalPrice = totalPrice.add(itemService.getItem(itemId).getListPrice());
        }
        orders.setTotalPrice(totalPrice);
        ordersDAO.save(orders);
    }
    public List<Orders> getAllByUsername(String userId){
        return ordersDAO.findByUserId(userId);
    }
    public List<Orders> getAll(){
        return ordersDAO.findAll();
    }
    public Orders getOrderByOrderId(String orderId){return ordersDAO.findByOrderId(orderId);}
}
